package com.ybkj.o2o.mc.controller.res;


import com.google.common.base.Preconditions;

import com.ybkj.o2o.mc.common.PhoneNumberHelper;
import com.ybkj.o2o.mc.common.transport.Error;
import com.ybkj.o2o.mc.common.transport.Result;
import com.ybkj.o2o.mc.entity.Passenger;
import com.ybkj.o2o.mc.entity.PassengerConcats;
import com.ybkj.o2o.mc.entity.PassengerGrade;
import com.ybkj.o2o.mc.manager.PassengerConcatsManager;
import com.ybkj.o2o.mc.manager.PassengerManager;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by shine on 15/8/25.
 */
@RestController
@RequestMapping("api/passengers")
public class PassengerConcatsRes {

  @Autowired
  private PassengerConcatsManager passengerConcatsManager;

  @Autowired
  private PassengerManager passengerManager;

  /**
   * 创建紧急联系人.
   */
  @RequestMapping(value = {"{passengerId}/concats"}, method = RequestMethod.POST)
  public ResponseEntity<Object> create(@PathVariable("passengerId") Long passengerId,
                                       @RequestParam("name") String name,
                                       @RequestParam("phone") String phone,
                                       @RequestParam("appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;

    name = StringUtils.trimToEmpty(name);
    phone = StringUtils.trimToEmpty(phone);
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(phone), "联系人电话不能为空");
    Preconditions.checkArgument(PhoneNumberHelper.isPhoneNumber(phone), "电话格式不正确");
    Preconditions.checkArgument(StringUtils.isNotBlank(name), "联系人姓名不能为空");
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Preconditions.checkArgument(passengerId != null && passengerId != 0, "乘客Id不能为空");
    Passenger passenger = passengerManager.findByIdAndAppKey(passengerId, appKey);
    Preconditions.checkArgument(passenger != null, "此乘客不存在");
    //判断是否存在
    boolean exists = passengerConcatsManager.exists(phone, passengerId);
    Preconditions.checkArgument(!exists, "此联系人已经存在");

    //保存
    try {

      PassengerConcats passengerConcats = new PassengerConcats();
      passengerConcats.setName(name);
      passengerConcats.setPhone(phone);
      passengerConcats.setPassengerId(passengerId);
      passengerConcatsManager.insert(passengerConcats);

      Result<PassengerConcats> result = new Result<>(0, "成功", passengerConcats);

      responseEntity = new ResponseEntity<>(result, HttpStatus.CREATED);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);
      return responseEntity;
    }

  }

  /**
   * 删除紧急联系人.
   */
  @RequestMapping(value = "{passengerId}/concats/{concatsId}", method = RequestMethod.DELETE)
  public ResponseEntity<Object> delete(@PathVariable("passengerId") Long passengerId,
                                       @PathVariable("concatsId") Long concatsId,
                                       @RequestParam("appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;

    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Preconditions.checkArgument(passengerId != null && passengerId != 0, "乘客Id不能为空");
    Preconditions.checkArgument(concatsId != null && concatsId != 0, "联系人Id不能为空");
    Passenger passenger = passengerManager.findByIdAndAppKey(passengerId, appKey);
    Preconditions.checkArgument(passenger != null, "此乘客不存在");

    //删除
    try {
      passengerConcatsManager.deleteByIdAndPassengerId(concatsId, passengerId);
      Result<PassengerGrade> result = new Result<>(0, "成功", null);

      responseEntity = new ResponseEntity<>(result, HttpStatus.NO_CONTENT);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);
      return responseEntity;
    }
  }

  /**
   * 修改紧急联系人.
   */
  @RequestMapping(value = "{passengerId}/concats/{concatsId}", method = RequestMethod.PUT)
  public ResponseEntity<Object> update(@PathVariable("concatsId") Long concat,
                                       @PathVariable("passengerId") Long passengerId,
                                       @RequestParam(required = false, value = "name") String name,
                                       @RequestParam("phone") String phone,
                                       @RequestParam("appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;

    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Preconditions.checkArgument(passengerId != null && passengerId != 0, "乘客Id不能为空");
    Preconditions.checkArgument(concat != null && concat != 0, "联系人Id不能为空");
    Preconditions.checkArgument(PhoneNumberHelper.isPhoneNumber(phone), "电话格式不正确");
    Passenger passenger = passengerManager.findByIdAndAppKey(passengerId, appKey);
    Preconditions.checkArgument(passenger != null, "此乘客不存在");
    //判断是否存在
    PassengerConcats local = passengerConcatsManager.findByConcatsAndPassenger(concat, passengerId);
    Preconditions.checkArgument(local != null, "联系人不存在");
    try {
      local.setName(name);
      local.setPhone(phone);
      passengerConcatsManager.update(local);
      Result<Passenger> result = new Result<>(0, "成功", null);

      responseEntity = new ResponseEntity<>(result, HttpStatus.CREATED);

      return responseEntity;
    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }

  /**
   * 查询乘客的联系人.
   */
  @RequestMapping(value = {"{passengerId}/concats"}, method = RequestMethod.GET)
  public ResponseEntity<Object> findConcats(@PathVariable("passengerId") Long passengerId,
                                            @RequestParam("appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Preconditions.checkArgument(passengerId != null && passengerId != 0, "乘客Id不能为空");
    Passenger passenger = passengerManager.findByIdAndAppKey(passengerId, appKey);
    Preconditions.checkArgument(passenger != null, "此乘客不存在");
    try {
      List<PassengerConcats> list = passengerConcatsManager.findByPassengerId(passengerId);

      Result<List<PassengerConcats>> result = new Result<>(0, "成功", list);

      responseEntity = new ResponseEntity<>(result, HttpStatus.OK);

      return responseEntity;
    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }
}
